<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_entry extends CI_Model{
	function getOwner($task = null){
		$sql = "SELECT owner_id, owner_nama FROM m_owner";
		$query = $this->db->query($sql);
		$result = $query->result();
		$data = null;
		switch($task){
			case "combo" : 
				$data = array( "0" => "-");
				foreach($result as $row){
					$data[$row->owner_id] = $row->owner_nama;
				}
			break;
			default :
				$data = $result;
		}
		return $data;
	}
	function getCabang($task = null){
		$sql = "SELECT cabang_id, cabang_nama FROM m_cabang";
		$query = $this->db->query($sql);
		$result = $query->result();
		$data = null;
		switch($task){
			case "combo" : 
				$data = array( "0" => "-");
				foreach($result as $row){
					$data[$row->cabang_id] = $row->cabang_nama;
				}
			break;
			default :
				$data = $result;
		}
		return $data;
	}
	function getKondisi($task = null){
		$sql = "SELECT kondisi_id, kondisi_nama FROM m_kondisi";
		$query = $this->db->query($sql);
		$result = $query->result();
		$data = null;
		switch($task){
			case "combo" : 
				$data = array( "0" => "-");
				foreach($result as $row){
					$data[$row->kondisi_id] = $row->kondisi_nama;
				}
			break;
			default :
				$data = $result;
		}
		return $data;
	}
	function getPic($task = null){
		$sql = "SELECT pic_id, pic_name FROM m_pic";
		$query = $this->db->query($sql);
		$result = $query->result();
		$data = null;
		switch($task){
			case "combo" : 
				$data = array( "0" => "-");
				foreach($result as $row){
					$data[$row->pic_id] = $row->pic_name;
				}
			break;
			default :
				$data = $result;
		}
		return $data;
	}
	function list_jadwal($option,$id = 0,$page=1,$rows=10,$jadwal_namaproyek ='',$tipe = "B"){
		if ($option == "single"){
			$sql = "
			SELECT * FROM t_jadwal 
			where jadwal_id = '".mysql_real_escape_string($id)."'
			ORDER BY jadwal_id DESC ";
			$query = $this->db->query($sql);
			$result = $query->result();
			return json_encode($result);
		}else{
			$sql = "
			SELECT
			if (lower(kondisi_nama) = 'selesai' , 'S', '') as jadwal_flag,
			jadwal_id, jadwal_namaproyek ,jadwal_tglaan, IF (kondisi_nama ='PQ', concat('(PQ) ',jadwal_tglpq) ,jadwal_tglpemasukan) as  jadwal_tglpemasukan, jadwal_tglklarifikasi ,jadwal_tglkontrak,
			concat('<span class=\"btn-group\"><input type=button class=\"btn btn-info\" value=\"Edit\" onClick=\"edit_entry(',jadwal_id,')\" /><input type=button class=\"btn btn-error\" value=\"Delete\" onClick=\"hapus_entry(',jadwal_id,')\" /> </span>') as action
			FROM t_jadwal LEFT JOIN m_kondisi
			ON m_kondisi_id = kondisi_id
			WHERE ";
			
			IF ($jadwal_namaproyek != ""){
				$sql .= "jadwal_namaproyek like '%".$jadwal_namaproyek."%' AND ";
				$tipe = "A";
			}
			
			switch ($tipe){
				case "A" : $sql .= "  true "; break;
				case "S" : $sql .= " lower(kondisi_nama) = 'selesai' "; break;
				case "B" : $sql .= " lower(kondisi_nama) <> 'selesai' ";break;
				default : $sql .= " lower(kondisi_nama) <> 'selesai' ";
			}
			 
			$sql .= "
			ORDER BY jadwal_id DESC ";
			
			
			$query = $this->db->query($sql);
			$records = $query->num_rows();
			$this->firephp->log($sql);
			$sql = "SELECT * FROM (".$sql.") k LIMIT ".(($page - 1) * $rows).",".$rows;
			$query = $this->db->query($sql);
			$result = $query->result();
			// '.ceil($records - $rows).'
			return '{ 
			  "total": "'.ceil($records / $rows).'", 
			  "page": "'.$page.'", 
			  "records": "'.$records .'",
			  "rows" : '.json_encode($result).'
			}';
		}
	}
	
	function save($id,$data){
		if ($id == ""){
			$this->db->insert('t_jadwal',$data);
		}else{
			$this->db->where('jadwal_id',$id);
			$this->db->update('t_jadwal',$data);
		}
		echo 1;
	}
	
	function jadwalhistory($data){
		$this->db->insert('t_jadwalhistory',$data);
	}
	
	function delete($data){
		$this->db->delete('t_jadwal',$data);
		echo 1;
	}
	
	function getRevisiKe($jadwal_id){
		$this->db->where("t_jadwal_id",$jadwal_id);
		$this->db->select("MAX(revisi_ke) as revisi_max",false);
		$query = $this->db->get('t_revisi');
		$revisi_max = $query->row("revisi_max");
		return $revisi_max + 1;
	}
	
	function revisi($data){
		$this->db->insert('t_revisi',$data);
		//echo 1;
	}
	
	function updatenk($jadwal_id,$data){
		$this->db->where('jadwal_id',$jadwal_id);
		$this->db->update('t_jadwal',$data);
	}
    
	function history($data){
		$this->db->insert('t_history',$data);
		extract($data);
		$data_update = array();
		switch ($history_jenistgl){
			case 1: $data_update["jadwal_tglpemasukan"] = $history_tgl ; break;
			case 2: $data_update["jadwal_tglpemasukan2"] = $history_tgl ; break;
			case 3: $data_update["jadwal_tglpq"] = $history_tgl ; break;
			case 4: $data_update["jadwal_tglaan"] = $history_tgl ; break;
			case 5: $data_update["jadwal_tglklarifikasi"] = $history_tgl ; break;
		}
		
		$this->db->where("jadwal_id",$t_jadwal_id);
		$this->db->update("t_jadwal",$data_update);
	}
	
        
	function revisiubah($data,$revisi_id,$historyidter,$jadwal_nk){
        $this->db->where("t_revisi_id",$revisi_id);
		$this->db->update('t_revisi',$data);
        
        if ($historyidter == $revisi_id){
                
            extract($data);
            $data_update = array(
                "jadwal_nk" => $revisi_harga
            );
            
            $this->db->where("jadwal_id",$t_jadwal_id);
            $this->db->update("t_jadwal",$data_update);
            
            echo number_format($revisi_harga,0,".",",");
        }else{
        
            echo number_format($jadwal_nk,0,".",",");
        }
	}
    
	function ubahhistory($data,$historyid,$historyidter,$tgllama){
        $this->db->where("history_id",$historyid);
		$this->db->update('t_history',$data);
        
        if ($historyidter == $historyid){
                
            extract($data);
            $data_update = array();
            switch ($history_jenistgl){
                case 1: $data_update["jadwal_tglpemasukan"] = $history_tgl ; break;
                case 2: $data_update["jadwal_tglpemasukan2"] = $history_tgl ; break;
                case 3: $data_update["jadwal_tglpq"] = $history_tgl ; break;
                case 4: $data_update["jadwal_tglaan"] = $history_tgl ; break;
                case 5: $data_update["jadwal_tglklarifikasi"] = $history_tgl ; break;
            }
            
            $this->db->where("jadwal_id",$t_jadwal_id);
            $this->db->update("t_jadwal",$data_update);
            
             $result = array(
                "status" => "success",
                "tgl" => date("Y-m-d",strtotime($history_tgl))
            );
            echo json_encode($result);
        }else{
        
             $result = array(
                "status" => "success",
                "tgl" => date("Y-m-d",strtotime($tgllama))
            );
            echo json_encode($result);
        }
	}
    
	function getLastRevisi($jadwal_id){
		$sql  = "SELECT * FROM t_jadwal
			LEFT OUTER JOIN t_revisi ON jadwal_id = t_jadwal_id
			WHERE jadwal_id = '".mysql_real_escape_string($jadwal_id)."'
			ORDER BY revisi_ke DESC" ;
		$query = $this->db->query($sql);
		$row = $query->row();
		$data = array();
		if ($row->t_revisi_id == ""){
			$data["revisi_harga"] = $row->jadwal_nk;
			$data["revisi_tgl"] = "";
			$data["revisi_keterangan"] = "";
		}else{
			$data["revisi_harga"] = $row->revisi_harga;
			$data["revisi_tgl"] = $row->revisi_tgl;
			$data["revisi_keterangan"] = $row->revisi_keterangan;
		}
		return $data;
	}
	
	function getLastHistory($jadwal_id, $history_jenistgl){
        if ($history_jenistgl == 0){
            
            $sql  = "SELECT * FROM t_revisi
			WHERE t_jadwal_id = '".mysql_real_escape_string($jadwal_id)."'
			ORDER BY revisi_ke ASC" ;
        }else{
		$sql  = "SELECT * FROM `t_history` where t_jadwal_id = '".mysql_real_escape_string($jadwal_id)."'
            AND history_jenistgl = '".mysql_real_escape_string($history_jenistgl)."'
			ORDER BY history_tglcreated ASC" ;
        }
		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}
    
	function datagrid($id =0){
		$sql = "SELECT * FROM t_jadwal WHERE jadwal_id ='".mysql_real_escape_string($id)."'";
		$query = $this->db->query($sql);
		$row = $query->row();
		return $row;
	}
	
	function _owner(){
		$sql = "SELECT owner_id, owner_nama FROM m_owner WHERE owner_aktif = 'Y' ";
		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}
}